﻿using Itech_Run.Application.Shared;
using Itech_Run.Sys.Application.Contracts.Dto;
using Itech_Run.Sys.Application.Contracts.Service;
using Itech_Run.Sys.Domain.Entity;
using Itech_Run.Sys.Domain.IRepository;
using Volo.Abp.Uow;

namespace Itech_Run.Sys.Application.Service
{
    public class SysRoleModuleButtonService : BaseApplicationService<Sys_RoleModuleButton>, ISysRoleModuleButtonService
    {
        private readonly ISys_RoleModuleButtonRepository _sys_RoleModuleButtonRepository;

        public SysRoleModuleButtonService(
            ISys_RoleModuleButtonRepository sys_RoleModuleButtonRepository)
        {
            _sys_RoleModuleButtonRepository = sys_RoleModuleButtonRepository;
        }

        [UnitOfWork(IsDisabled = false)]
        public async Task<Dictionary<string, List<ButtonAuthDto>>> GetUserRoleModuleButtonAuthAsync(string userId)
        {
            var result = new Dictionary<string, List<ButtonAuthDto>>();
            var dynamicResult = _sys_RoleModuleButtonRepository.GetControllerButtonAuth(userId);
            for (var i = 0; i < dynamicResult.Count; i++)
            {
                var item = dynamicResult[i];
                string controller = item.ApiController;
                if (result.ContainsKey(controller))
                {
                    List<ButtonAuthDto> dtos = result[controller];
                    dtos.Add(new ButtonAuthDto { ButtonCode = item.Code, HasAuth = item.HasAuth });
                    result[controller] = dtos;
                }
                else
                {
                    List<ButtonAuthDto> dtos = new List<ButtonAuthDto>();
                    dtos.Add(new ButtonAuthDto { ButtonCode = item.Code, HasAuth = item.HasAuth });
                    result[controller] = dtos;
                }
            }

            return result;
        }
    }
}
